﻿uniform sampler2D SourceMap;
uniform sampler2D ObjectMap;

uniform vec2 LightPos;
uniform vec3 LightCol;
uniform vec3 ShadowCol;
uniform float LightRange;

#define SAMPLES 256

void main(){

  vec3 src;

  float obj;

  src = texture2D( SourceMap,gl_TexCoord[0].st );
  //obj = texture2D( ObjectMap,gl_TexCoord[0].st );
  
  vec2 dir;

  dir.x = LightPos.x-gl_TexCoord[0].s;
  dir.y = LightPos.y-gl_TexCoord[0].t;

  float lc = length(dir);

  lc = lc / (LightRange*0.01f);

  if(lc>1.0){

   lc=0.0;

  }else{

   lc=1.0-lc;

  }

  dir.x/=SAMPLES;
  dir.y/=SAMPLES;



  int i=SAMPLES;

  vec2 obj_uv = gl_TexCoord[0].st;

  float shade = 1.0;

  while(i>0){

    obj = texture2D( ObjectMap,obj_uv ).r;
  
	if(obj>0.1){

		shade = 0.45;
	   break;

	}

	obj_uv+=dir;
    
    i--;
  
  }



  gl_FragColor.rgb = (lc*shade*0.25f)*LightCol;
  gl_FragColor.a = 1.0;

}
